<?php
namespace app\admin\controller;
class Upload extends Common {
	
	public function index(){
		return $this->fetch();
	}
	public function addImages(){
		
		$config = [
			'size' => $this->config['CFG_UPLOAD_MAXSIZE'],
			'ext' => $this->config['CFG_UPLOAD_FILE_EXT']
		];
		$file = request()->file('file');
		$url = 'uploads/thumb';
		$info = $file->validate($config)->move($url);
		if($info){
			$infoUrl = str_replace('\\', '/', $info->getSaveName());
			$rest = $url.'/'.$infoUrl;
			return show(0,"上传成功",$rest,200);
		}else{
			// 上传失败获取错误信息
			return show(1,$file->getError(),0,200);
		}
	}
	public function addPics(){
		$files = request()->file('file');
		$url = 'uploads/thumb';
		$pics = [];
		$config = [
			'size' => $this->config['CFG_UPLOAD_MAXSIZE'],
			'ext' => $this->config['CFG_UPLOAD_FILE_EXT']
		];
		foreach($files as $key=>$file){
			// 移动到框架应用根目录/uploads/ 目录下
			$info = $file->validate($config)->move($url);
			if($info){
				$infoUrl = str_replace('\\', '/', $info->getSaveName());
				$pics[$key] = $url.'/'.$infoUrl;
			}else{
				// 上传失败获取错误信息
				return show(1,$file->getError(),$rest,200);
			}    
		}
		return show(0,"上传成功",$pics,200);
	}
	
	public function addKindedit(){
		
		$config = [
			'size' => $this->config['CFG_UPLOAD_MAXSIZE'],
			'ext' => $this->config['CFG_UPLOAD_FILE_EXT']
		];
		$file = request()->file('imgFile');
		$url = 'uploads/image';
		$info = $file->validate($config)->move($url);
		if($info){
			$infoUrl = str_replace('\\', '/', $info->getSaveName());
			$rest = '/'.$url.'/'.$infoUrl;
			$res = [
				'url'=>$rest,
				'error' => 0
			];
			return json($res);
		}else{
			// 上传失败获取错误信息
			$res = [
				'message'=>$file->getError(),
				'error' => 1
			];
			return json($res);
		}
	}
}